package org.ncibi.cytoscape.metscape.ui;

import giny.view.NodeView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.ncibi.cytoscape.metscape.data.Attributes;
import org.ncibi.cytoscape.metscape.task.BuildSubnetworkTask;
import org.ncibi.cytoscape.metscape.task.CollapseNetworkTask;
import org.ncibi.cytoscape.metscape.task.ExpandInExistingNetworkTask;
import org.ncibi.cytoscape.metscape.task.ExpandInSubnetworkTask;
import org.ncibi.cytoscape.metscape.task.RestoreNetworkTask;

import cytoscape.CyNetwork;
import cytoscape.util.CyNetworkNaming;
import ding.view.NodeContextMenuListener;

public class PopupNodeContextMenuListener implements NodeContextMenuListener {
	public void addNodeContextMenuItems (NodeView nodeView, JPopupMenu menu) {
		final String id = nodeView.getNode().getIdentifier();
		final CyNetwork network = (CyNetwork) nodeView.getGraphView().getGraphPerspective();
		if(!Attributes.node.hasAttribute(id, "Category."+ network.getTitle()))
			return;
		
		if(menu == null){
			menu=new JPopupMenu();
		}
		
		JMenu metscapeMenu = new JMenu("MetScape");
		JMenuItem createSubnetwork = new JMenuItem("Create Subnetwork");
		if(network.getSelectedNodes().isEmpty())
			createSubnetwork.setEnabled(false);
		createSubnetwork.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent ae){         			
    			String subnetworkName = CyNetworkNaming.getSuggestedSubnetworkTitle(network);
    			BuildSubnetworkTask.newSubnetwork
    				(network.getSelectedNodes(), network.getSelectedEdges(), subnetworkName, network);
    		}
    	});
		metscapeMenu.add(createSubnetwork);
		
		JMenu expandMenu = new JMenu("Expand");
		if(!Attributes.node.getStringAttribute(id, "Type").equals("Compound") ||
				Attributes.node.getBooleanAttribute(id, "isExpansionSeed." + network.getTitle()))
			expandMenu.setEnabled(false);
		JMenuItem expandInExistingNetwork = new JMenuItem("Expand in Existing Network");
		expandInExistingNetwork.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent ae){         			
    			ExpandInExistingNetworkTask.expandUsing(id, network);
    		}
    	});
		expandMenu.add(expandInExistingNetwork);
		JMenuItem expandInSubnetwork = new JMenuItem("Expand in Subnetwork");
		expandInSubnetwork.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent ae){         			
    			ExpandInSubnetworkTask.expandUsing(id, network);
    		}
    	});
		expandMenu.add(expandInSubnetwork);
		metscapeMenu.add(expandMenu);
		
		JMenuItem collapse = new JMenuItem("Collapse");
		if(!Attributes.node.getBooleanAttribute(id, "isExpansionSeed." + network.getTitle()))
			collapse.setEnabled(false);
		collapse.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent ae){
    			CollapseNetworkTask.collapseCyNetwork(network, id);
    		}
    	});
		metscapeMenu.add(collapse);
		JMenuItem restoreOriginalNetwork = new JMenuItem("Restore Original Network");
		restoreOriginalNetwork.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent ae){         			
    			RestoreNetworkTask.restoreCyNetwork(network);
    		}
    	});
		metscapeMenu.add(restoreOriginalNetwork);  
		menu.add(metscapeMenu);
	}

}
